import type { SoElements } from '../../../../../../../../types/constants/personalization/so'
import type { ThemeType } from '../../../../../../../../types/interfaces'
import { ThemeVarService } from '../../../../../../../../lib/abstract/style/theme_var/service'
import {
  AppStyleCollections,
  generateHideSelector,
  PAGE_ELEMENTS,
  SUPPORTED_PLATFORMS,
} from '../../../../../../../../types/constants'
import { PageBaseStyles } from '../../../base'
import { SoCommonPage } from './common'

export class SoIndexWenkuPage {
  elements: SoElements = PAGE_ELEMENTS[SUPPORTED_PLATFORMS.SO]
  private static instance: SoIndexWenkuPage
  private readonly themeVarService: ThemeVarService = ThemeVarService.getInstance()
  private readonly pageBaseStyles: PageBaseStyles = PageBaseStyles.getInstance()
  private readonly commonPage: SoCommonPage = SoCommonPage.getInstance()

  /**
   * 获取单例实例
   */
  public static getInstance(): SoIndexWenkuPage {
    if (!SoIndexWenkuPage.instance) {
      SoIndexWenkuPage.instance = new SoIndexWenkuPage()
    }
    return SoIndexWenkuPage.instance
  }

  init(): void {
  }

  hideSearchStyles(): string {
    return `
    + .wenku-index__queryList {
      display: none !important;
    }
    `
  }

  getMainStyles(theme: ThemeType): string {
    return `
  /* 隐藏样式 */
  #app #wenku-index .so-nav-container:has(${generateHideSelector('ul')}):has(${generateHideSelector('.top-nav-right')}) {
    background-color: transparent !important;
  }
  #app #wenku-index .so-nav-container:has(${generateHideSelector('.top-nav-right')}) {
    ul {
      display: flex !important;
      justify-content: center !important;
      align-items: center !important;
      padding: 0 !important;
    }
  }
  
  /* 搜索历史隐藏、搜索建议隐藏 */
  &[${AppStyleCollections.Search.attr.HIDE_HISTORY}="true"][${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}="true"] {
    form {
      ${this.hideSearchStyles()}
    }
  }
  /* 搜索历史隐藏、搜索建议显示 */
  &[${AppStyleCollections.Search.attr.HIDE_HISTORY}="true"]:not([${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}]) {
    /* 无搜索建议 */
    form[${AppStyleCollections.Search.attr.IS_SUGGESTIONS_EMPTY}="true"] {
      ${this.hideSearchStyles()}
    }
  }
  /* 搜索历史显示、搜索建议隐藏 */
  &:not([${AppStyleCollections.Search.attr.HIDE_HISTORY}])[${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}="true"] {
    /* 无搜索历史 */
    form[${AppStyleCollections.Search.attr.IS_HISTORY_EMPTY}="true"] {
      ${this.hideSearchStyles()}
    }
  }

  /* 主样式 */  
  #app {
    #wenku-index {
      background: transparent !important;
      
      /* 顶部菜单*/
      .so-nav-container {
        background-color: var(${this.themeVarService.search.header.background}) !important;

        a {
          color: var(${this.themeVarService.search.default.text}) !important;
        }
        
        li {
          &.so-nav-container__item {
            &:hover {
              a {
                color: var(${this.themeVarService.tab.textHover}) !important;
              }
            }
            
            &.activated-tab {
              &::after {
                height: 2px !important;
                background-color: var(${this.themeVarService.tab.textActive}) !important;
              }

              a {
                color: var(${this.themeVarService.tab.textActive}) !important;
              }
            }
          }
        }
        
        .top-nav-right {
          a {
            &:hover {
              color: var(${this.themeVarService.link.default.text}) !important;
            }
          }

          .top-nav-vip {
            .vip-entrance__text {
              color: var(${this.themeVarService.search.default.text}) !important;
              
              &:hover {
                color: var(${this.themeVarService.link.default.text}) !important;
              }
            }
          }

          .top-nav-login {
            .so-header-menu-item__title {
              &:hover {
                span {
                  color: var(${this.themeVarService.link.default.text}) !important;
                }
              }

              span {
                color: var(${this.themeVarService.search.default.text}) !important;
              }
            }
          }
        }
      }
      
      /* 主内容 */
      .wenku-index__content {
        #wenku-index__inputBox__content__search .wenku-index__queryList {
          /* 搜索历史高亮 */
          .wenku-index__queryList__list__logItem {
            color: var(${this.themeVarService.search.history.text}) !important;
            
            &:hover {
              color: var(${this.themeVarService.link.default.text}) !important;
            }
          }
          
          /* 搜索建议高亮 */
          .wenku-index__queryList__list__item {
            color: var(${this.themeVarService.search.suggestions.text}) !important;
            font-weight: ${this.themeVarService.search.suggestions.fontWeight} !important;
            
            &:hover {
              color: var(${this.themeVarService.link.default.text}) !important;
            }
            
            span {
              color: var(${this.themeVarService.em.text}) !important;
            }
          }
        }

        .wenku-index__aiCreate, .wenku-index__recommend, .wenku-index__convert {
          .wenku-index__tabs {
            .wenku-index__tabs__title {
              ${this.pageBaseStyles.titleStyles()}
            }
            
            .wenku-index__tabs__list {
              .wenku-index__tabs__list__item {
                &:hover {
                  color: var(${this.themeVarService.link.default.text}) !important;
                }
              }
            }
          }
        }
        
        .wenku-index__aiCreate {
          .wenku-index__cardList {
            .wenku-index__cardList__item {
              opacity: ${theme === 'dark' ? 0.8 : 1} !important;
            }
          }
        }
        
        .wenku-index__recommend {
          .wenku-index__docList {
            .wenku-index__docList__item__imgContainer {
              border-color: var(${this.themeVarService.default.border.colorNeutral}) !important;

              img {
                ${this.pageBaseStyles.imgStyles(theme)}
              }
            }
            
            .wenku-index__docList__item__title {
              color: var(${this.themeVarService.link.default.text}) !important;
              
              &:hover {
                color: var(${this.themeVarService.link.default.textHover}) !important;
              }
            }
          }
        }

        .wenku-index__convert {
          .wenku-index__convert__title {
            ${this.pageBaseStyles.titleStyles()}
          }
          
          .wenku-index__toolbox__item {
            &:hover {
              .wenku-index__toolbox__item__text__title {
                color: var(${this.themeVarService.link.default.text}) !important;
              }
            }
          
            img {
              ${this.pageBaseStyles.imgStyles(theme)}
            }

            .wenku-index__toolbox__item__text__title {
              color: var(${this.themeVarService.search.default.text}) !important;
            }
          }
        }
      }
      
      /* 页脚 */
      ${this.commonPage.footStyles()}
    }
  }
`
  }
}
